import {Component, OnInit} from "@angular/core";
import {UserService} from "../../UserService";
import {LocalDataSource} from "ng2-smart-table";
import {BillService, DocService, PowerService} from "../../energy/api";
import {BaseRequest} from "../../utils/BaseRequest";
import {UserInfo} from "../../energy/api/model/userInfo";
import {UnitInfo} from "../../energy/api/model/unitInfo";
import {PowerExtraService} from "../../energy/api/api/powerExtra.service";
import {start} from "repl";
import {NbDateService} from "@nebular/theme";
import {BillResult} from "../../energy/api/model/billResult";

@Component({
  templateUrl: './user-profile.component.html',
  styles:[
    `
      
    `
  ]
})
export class UserProfileComponent implements OnInit{
  user:UserInfo;
  unitSettings = {
    hideSubHeader: true,
    actions: {
      add:false,
      edit:false,
      delete:false,
    },
    columns: {
      unitId: {
        title: 'ID',
        type: 'number',
      },
      unitNo: {
        title: '单元编号',
        type: 'string',
      },
      unitName: {
        title: '单元名称',
        type: 'string',
      },
      address: {
        title: '地址',
        type: 'string',
      },
      grossArea: {
        title: '均摊面积',
        type: 'string',
      },
      netArea: {
        title: '面积',
        type: 'string',
      },
      floorName: {
        title: '楼层名称',
        type: 'string',
      },
      extInfo: {
        title: '额外信息',
        type: 'string',
      },
      device: {
        title: '设备列表',
        type: 'html',
      }
    },
  };
  source: LocalDataSource = new LocalDataSource();
  unitInfo:UnitInfo;
  monthAmt:number = 0;
  monthUsage:number = 0;
  lastBillResult:BillResult;
  constructor(private userService:UserService,private api:DocService,private billApi:BillService,private powerApi:PowerExtraService,private dateService: NbDateService<Date>){
    this.user = userService.getUser();
    console.log(this.user);
  }

  ngOnInit(): void {
    const startDate = new Date();
    startDate.setDate(0);
    const endDate = new Date();
    this.billApi.requestECABillResult(new BaseRequest({
      pageIndex:0,
      pageSize:1,
      unitId:this.user.unitId
    })).subscribe(response=>{
      if(response.dataCount>0){
        this.lastBillResult = response.data[0]
      }
    });
    this.billApi.requestECAUnitChargeBill(new BaseRequest({
      pageIndex:0,
      pageSize:50,
      startTime:this.dateService.format(startDate,"yyyyMMdd"),
      endTime:this.dateService.format(endDate,"yyyyMMdd"),
      unitId:this.user.unitId
    })).subscribe(response=>{
      if(response.dataCount>0){
        let sum = 0;
        response.data.forEach(value=>{
          sum+=value.totalAmt;
        });
        this.monthAmt = sum;
      }
    });
    this.powerApi.requestDailyElectrcConsumptionData(new BaseRequest({
      pageIndex:0,
      pageSize:50,
      startTime:this.dateService.format(startDate,"yyyyMMdd"),
      endTime:this.dateService.format(endDate,"yyyyMMdd"),
      unitId:this.user.unitId
    })).subscribe(response=>{
      if(response.dataCount>0){
        let sum = 0;
        response.data.forEach(value=>{
          sum+=value.totalConsumption;
        });
        this.monthUsage = sum;
      }
    });
    this.api.requestECAUnitInfo(new BaseRequest(
      {
        pageIndex:0,
        pageSize:1,
        unitId:this.user.unitId
      }
    )).subscribe(response=>{
      if(response.dataCount>0) {
        this.unitInfo = Object.assign(response.data[0]);
        this.source.load(response.data.map(value => {
          this.user.unitId = value.unitId;
          value['device'] = `<a href="/#/pages/usr/dev">查看设备</a>`;
          return value;
        }));
      }
    })
  }
}
